package com.cn.yingu.service;

import com.cn.yingu.dao.VersionInfoDao;
import com.cn.yingu.pojo.VersionInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class VersionInfoService implements Serializable {
    @Autowired
    private VersionInfoDao dao;

    public List<VersionInfo> ListPage(Map<String, Object> searchParams) {
        List<VersionInfo> list = dao.ListPage(searchParams);

        VersionInfo[] arrList = new VersionInfo[list.size()];
        arrList = list.toArray(arrList);

        for (int i = 0; i < arrList.length; i++) {
//            list[i].SubmitDate = list[i].SubmitTime.Date;
            if (arrList[i].SubmitTime == null)
                continue;
            Date SubmitTime = arrList[i].SubmitTime;
            arrList[i].SubmitDate = new Date(SubmitTime.getYear(), SubmitTime.getMonth(), SubmitTime.getDate());
            if (i == 0) {
                arrList[i].Number = 1;
            } else {
                if (arrList[i].SubmitDate.getTime() == arrList[i - 1].SubmitDate.getTime()) {
                    arrList[i].Number = arrList[i - 1].Number + 1;
                } else {
                    arrList[i].Number = 1;
                }
            }
        }

        return Arrays.asList(arrList);
    }
    public int ListPageCount(Map<String, Object> searchParams){
        return dao.ListPageCount(searchParams);
    }

    public int Add(VersionInfo model) {
        model.DataStatus = 0;
        model.SubmitTime = new Date();
        dao.Add(model);
        return model.Id;
    }

    public Boolean Delete(int Id) {
        int result = dao.Delete(Id);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }
}
